//
//  WCDefaultManager.m
//  WC
//
//  Created by 杨鑫 on 15/5/8.
//  Copyright (c) 2015年 智康. All rights reserved.
//

#import "WCDefaultManager.h"
#import "WCCoreDataStore.h"
#import "WCNConstantsCDStore.h"
#import "WCImageManager.h"
#import "WCNPListFileDataStore.h"
//
#import "WCNetworkAccessManager.h"
#import "Constants.h"

@interface WCDefaultManager ()

@property (strong, nonatomic) Account *account;



@end

@implementation WCDefaultManager

+ (instancetype)shareMg
{
    static WCDefaultManager *sharedMg = nil;
    //block
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedMg = [[WCDefaultManager alloc] initPrivate];
        
    });
    return sharedMg;
}

- (instancetype)init
{
    @throw [NSException exceptionWithName:@"Singleton"
                                   reason:@"Use + [WCDefaultManager sharedMg]"
                                 userInfo:nil];
    return nil;
}

- (instancetype)initPrivate
{
    self = [super init];
    if (self) {
         _account = [[WCCoreDataStore shareStore] getAccount];
    }
    return self;
}

- (NSString *)lastAccount
{
    return [[NSUserDefaults standardUserDefaults] valueForKey:@"loginName"];
}
- (NSInteger)nurseId{
    Account *user = [self account];
    
    if (!user) {
        
        return 0;
    }
    
    return [user.stringId integerValue];
    
}
- (Account *)account
{
    return _account;
}

- (AccountEntity *)accountEntity
{
    return [[AccountEntity alloc] initWithAccount:self.account];
}

- (void)updateAccount:(AccountEntity *)accountEntity
{
    self.account.name = accountEntity.name;
    self.account.sex = accountEntity.sex;
    self.account.mobile = accountEntity.mobile;
    self.account.mail = accountEntity.mail;
    self.account.intro = accountEntity.intro;
    self.account.birthday = accountEntity.birthday;
    self.account.hospitalId = accountEntity.hospitalId;
    self.account.hospitalName = accountEntity.hospitalName;
    self.account.wardId = accountEntity.wardId;
    self.account.wardName = accountEntity.wardName;
}

- (void)updateAccountWithDic:(NSDictionary *)data
{
    self.account.name = data[@"name"];
    self.account.sex = data[@"sex"];
    self.account.mail = data[@"mail"];
    self.account.intro = data[@"intro"];
    self.account.birthday = data[@"birthday"];
    self.account.hospitalId = [NSString stringWithFormat:@"%@", data[@"hospitalId"]];
    self.account.hospitalName = data[@"hospitalName"];
    self.account.wardId = [NSString stringWithFormat:@"%@", data[@"wardId"]];
    self.account.wardName = data[@"wardName"];
}
- (void)updatePwdWithPwd:(NSString *)pwd{
    self.account.pwd = pwd;

}
- (void)login:(NSDictionary *)accout {
    
    _account = [[WCCoreDataStore shareStore] createAccount:accout];
    
   
    
}
- (void)updatSearchStatus:(NSInteger)searchStatus
{
    self.account.searchStatus =[NSString stringWithFormat:@"%i",searchStatus];
}
- (NSDictionary *)loginNameAndMd5Pwd
{
    
//    @"mobileNo": account, @"password":[password md5String]
    NSMutableDictionary *dict =[NSMutableDictionary dictionary];
    dict[@"mobileNo"] =_account.mobile;
    dict[@"password"] =_account.pwd;
    NSLog(@"+++++++++++++%@",dict);
    return dict;

}
- (void)logout
{
    _account = nil;
    
    //删除和护理师相关的数据
    [[WCCoreDataStore shareStore] removeAllData];
    //删除护理师和患者相关的图片
    [[WCImageManager sharedMg] deletePrivateImgs];
    [[WCImageManager sharedMg] removeImgsCache];
    //删除与患者相关的plist文件
    [[WCNPListFileDataStore shareStore] deletePrivateFiles];
    
    [[WCNetworkAccessManager sharedMg] resetTimeDic];
    [[NSNotificationCenter defaultCenter] postNotificationName:UserLogoutNotification object:nil userInfo:nil];
}

#pragma mark - App

- (BOOL)isFirstStart
{
    NSString *versionStr = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    return ![[NSUserDefaults standardUserDefaults] boolForKey:versionStr];
}

- (void)startApp
{
    NSString *versionStr = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:versionStr];
}

#pragma mark - Page Setting

- (void)saveSettings:(NSDictionary *)settings forPage:(NSString *)name
{
    [[NSUserDefaults standardUserDefaults] setValue:settings forKey:name];
}

- (NSDictionary *)pageSettings:(NSString *)name
{
    return [[NSUserDefaults standardUserDefaults] valueForKey:name];
}
- (void)saveLastAccouts:(NSString *)phone name:(NSString *)name{
   
    NSDictionary *dic =@{@"phone":phone,@"name":name};
    NSMutableArray *array =[NSMutableArray array];
    NSArray *oldArray =  [self getPhoneAccouts];
    if (oldArray) {
        [array addObjectsFromArray:oldArray];
        if (![oldArray containsObject:dic]) {
            [array insertObject:dic atIndex:0];
        }
    }else{
        [array addObject:dic];
    
    }
    
    if (array.count ==6) {
        [array removeLastObject];
    }
    [[NSUserDefaults standardUserDefaults] setObject:array forKey:@"LASTACCOUTS"];
    [[NSUserDefaults standardUserDefaults] synchronize];
   
    
}
- (NSArray *)getPhoneAccouts{
    
    NSArray *array  = [[NSUserDefaults standardUserDefaults] objectForKey:@"LASTACCOUTS"];
   
    return array;
}

- (void)saveLastMeetingAccouts:(NSString *)meetingAccount {
    
//    NSDictionary *dic =@{@"phone":phone,@"name":name};
    NSMutableArray *array =[NSMutableArray array];
    NSArray *oldArray =  [self getMeetingAccounts];
    if (oldArray) {
        [array addObjectsFromArray:oldArray];
        if (![oldArray containsObject:meetingAccount]) {
            [array insertObject:meetingAccount atIndex:0];
        }
    }else{
        [array addObject:meetingAccount];
        
    }
    
    if (array.count ==6) {
        [array removeLastObject];
    }
    [[NSUserDefaults standardUserDefaults] setObject:array forKey:@"MEETINGACCOUNTS"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    
}

- (NSArray *)getMeetingAccounts{
    NSArray *array  = [[NSUserDefaults standardUserDefaults] objectForKey:@"MEETINGACCOUNTS"];
    
    return array;

}


@end
